package com.yitong.equipment.ui.mall

import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.yitong.equipment.R
import com.yitong.equipment.base.BaseFragment
import com.yitong.equipment.databinding.FragmentMallAllClassifyBinding
import com.yitong.equipment.entity.other.CategoryOneArrayBean
import com.yitong.equipment.entity.other.CategoryTwoArrayBean
import com.yitong.equipment.entity.other.MallClassifyMultipleEntity
import com.yitong.equipment.entity.other.RightBean
import com.yitong.equipment.interf.CheckListener
import com.yitong.equipment.ui.adapter.ClassifyDetailAdapter
import com.yitong.equipment.ui.viewmodel.MallViewModel
import com.yitong.equipment.view.ItemHeaderDecoration
import java.util.*

class MallAllClassifyFragment : BaseFragment<MallViewModel, FragmentMallAllClassifyBinding>() ,CheckListener{
    private val classifyDetailAdapter: ClassifyDetailAdapter by lazy { ClassifyDetailAdapter(arrayListOf()) }
    private var checkListener: CheckListener? = null
    private val mDatas: MutableList<RightBean> = arrayListOf()
    private lateinit var mDecoration: ItemHeaderDecoration
    private lateinit var gridLayoutManager: GridLayoutManager
    private var mIndex = 0
    private var move = false

    override fun setContent(): Int {
        return R.layout.fragment_mall_all_classify
    }

    override fun initView() {
        gridLayoutManager = GridLayoutManager(activity, 3)
        //通过isTitle的标志来判断是否是title
        gridLayoutManager.spanSizeLookup = object : SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (mDatas[position].isTitle) 3 else 1
            }
        }
        bindingView.recycleView.layoutManager = gridLayoutManager
        bindingView.recycleView.adapter = classifyDetailAdapter
        mDecoration = activity?.let { ItemHeaderDecoration(it, mDatas) }!!
        bindingView.recycleView.addItemDecoration(mDecoration)
        mDecoration.setCheckListener(checkListener)
        bindingView.recycleView.addOnScrollListener(RecyclerViewListener())

        classifyDetailAdapter.setOnItemClickListener  { adapter, view, position ->
            if (view.id==R.id.root) {
                ToastUtils.showShort("当前点击的是title" +":" + mDatas.get(position).name)
            }else  if (view.id==R.id.content) {
                ToastUtils.showShort("当前点击的是content" +":" + mDatas.get(position).name)
            }
        }
    }

    override fun initData() {
        val rightList: ArrayList<CategoryOneArrayBean> = arguments?.getParcelableArrayList("right")!!
        println(rightList.toString())
        LogUtils.dTag("rightList",rightList.toString())

        var mutableListOf = mutableListOf<MallClassifyMultipleEntity>()

        for (i in rightList.indices) {
            val head = RightBean(rightList[i].name)
            //头部设置为true
            head.isTitle = true
            head.titleName = rightList[i].name
            head.tag = i.toString()
            mDatas.add(head)
            mutableListOf.add(MallClassifyMultipleEntity(MallClassifyMultipleEntity.LAYOUT_TITLE,head))
            val categoryTwoArray: List<CategoryTwoArrayBean> = rightList[i].categoryTwoArray!!
            for (j in categoryTwoArray.indices) {
                val body = RightBean(categoryTwoArray[j].name)
                body.tag = (i.toString())
                body.titleName = rightList[i].name
                mDatas.add(body)
                mutableListOf.add(MallClassifyMultipleEntity(MallClassifyMultipleEntity.LAYOUT_DETAIL,body))
            }
        }


        classifyDetailAdapter.setList(mutableListOf)
        mDecoration.setData(mDatas)
    }

    fun setData(n: Int) {
        mIndex = n
        bindingView.recycleView.stopScroll()
        smoothMoveToPosition(n)
    }

    private fun smoothMoveToPosition(n: Int) {
        val firstItem: Int = gridLayoutManager.findFirstVisibleItemPosition()
        val lastItem: Int = gridLayoutManager.findLastVisibleItemPosition()
        Log.d("first--->", firstItem.toString())
        Log.d("last--->", lastItem.toString())
        if (n <= firstItem) {
            bindingView.recycleView.scrollToPosition(n)
        } else if (n <= lastItem) {
            Log.d("pos---->", n.toString() + "VS" + firstItem)
            val top: Int = bindingView.recycleView.getChildAt(n - firstItem).getTop()
            Log.d("top---->", top.toString())
            bindingView.recycleView.scrollBy(0, top)
        } else {
            bindingView.recycleView.scrollToPosition(n)
            move = true
        }
    }

    fun setListener(listener: CheckListener) {
        this.checkListener = listener
    }

    override fun check(position: Int, isScroll: Boolean) {
        checkListener!!.check(position, isScroll)
    }

    override fun firstLoad() {
        viewModel.getData()
    }

    override fun notFirstLoad() {
        LogUtils.dTag("notFirstLoad", "notFirstLoad")
    }


    private inner class RecyclerViewListener : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            if (move && newState == RecyclerView.SCROLL_STATE_IDLE) {
                move = false
                val n: Int = mIndex - gridLayoutManager.findFirstVisibleItemPosition()
                Log.d("n---->", n.toString())
                if (0 <= n && n < bindingView.recycleView.childCount) {
                    val top: Int = bindingView.recycleView.getChildAt(n).top
                    Log.d("top--->", top.toString())
                    bindingView.recycleView.smoothScrollBy(0, top)
                }
            }
        }

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            if (move) {
                move = false
                val n: Int = mIndex - gridLayoutManager.findFirstVisibleItemPosition()
                if (0 <= n && n < bindingView.recycleView.childCount) {
                    val top: Int = bindingView.recycleView.getChildAt(n).top
                    bindingView.recycleView.scrollBy(0, top)
                }
            }
        }
    }


}